<template>
  <div id="wrapper" style="padding: 20px">
    <el-form :model="userInfo" status-icon label-width="100px" class="demo-ruleForm">
      <el-form-item label="头像" prop="avatar">
        <template slot-scope="porps">
          <el-upload
              class="avatar-uploader"
              :action="baseApi + '/api/personal/uploadAvatar'"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="userInfo.avatar != '' && userInfo.avatar != undefined" :src="userInfo.avatar" class="avatar"/>
            <img v-else src="../../assets/images/max.png" class="avatar"/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </template>
      </el-form-item>
      <el-form-item label="账号" prop="name">
        <el-input v-model.number="userInfo.name" disabled></el-input>
      </el-form-item>
      <el-form-item label="权限" prop="roleName">
        <el-input v-model.number="userInfo.roleName" disabled></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="realName">
        <el-input v-model.number="userInfo.realName"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="realName">
        <el-input v-model.number="userInfo.createTime" disabled></el-input>
      </el-form-item>
      <el-form-item label="最后登录时间" prop="realName">
        <el-input v-model.number="userInfo.loginTime" disabled></el-input>
      </el-form-item>
      <el-form-item label="操作" prop="action">
        <template slot-scope="porps">
          <el-link type="info" @click="showUpdatePassword">修改密码</el-link>
        </template>
      </el-form-item>
      <el-form-item align="right">
        <el-button type="primary" @click="submitForm()">提交</el-button>
      </el-form-item>
    </el-form>
    <el-dialog
        title="修改密码"
        :visible.sync="updatePasswordDialogVisible"
        width="50%">
      <el-form :model="updatePasswordQuery" status-icon label-width="100px" class="demo-ruleForm">
        <el-form-item label="旧密码" prop="name">
          <el-input v-model="updatePasswordQuery.oldPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="roleName">
          <el-input v-model="updatePasswordQuery.newPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="再次确认" prop="roleName">
          <el-input v-model="updatePasswordQuery.againNewPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item align="right">
          <el-button type="primary" @click="updatePassword()">确认修改</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "PersonalInformation",
  data: function() {
    return {
      baseApi: process.env.BASE_API,
      userInfo:{
        avatar: ""
      },
      updatePasswordDialogVisible: false,
      updatePasswordQuery: {
        oldPassword: '',
        newPassword: '',
        againNewPassword: ''
      }
    }
  },
  methods: {
    async getPersonalInformation() {
      let res = await this.$http.get('/api/personal/getPersonalInformation')
      this.userInfo = res.data
    },
    /**
     * 修改密码
     */
    async updatePassword() {
      if (this.updatePasswordQuery.newPassword !== this.updatePasswordQuery.againNewPassword) {
        this.$message.error('两次输入的密码不一致,请重新输入');
        return
      }
      let res = await this.$http.post('/api/personal/updatePassword', this.updatePasswordQuery)
      if (res.code == 0) {
        this.$message.success('修改成功');
        this.updatePasswordDialogVisible = false
        this.getPersonalInformation()
      }
    },
    /**
     * 显示修改密码框
     */
    showUpdatePassword() {
      this.updatePasswordDialogVisible = true
    },
    /**
     * 对图片进行校验
     * @param file
     * @returns {boolean}
     */
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 4;
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 4MB!');
      }
      return isJPG && isLt2M;
    },
    /**
     * 图片上传成功回调
     * @param res
     */
    handleAvatarSuccess(res) {
      this.userInfo.avatar = this.baseApi + res.data
    },
    /**
     * 提交修改
     */
    async submitForm() {
      let res = await this.$http.post('/api/personal/updateUser', this.userInfo)
      if (res.code == 0) {
        this.$message.success('修改成功');
        this.getPersonalInformation()
      }
    }
  },
  created: function(){
    this.getPersonalInformation()
  },
  mounted() {
    this.$bus.$on('page-refresh', name => {
      if (name === 'PersonalInformation') {
        this.getPersonalInformation()
      }
    })
  }
}
</script>

<style lang="scss" scoped>
.avatar {
  width: 12em;
}
</style>
